﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.Entities.BaseClasses;
using System.Configuration;

namespace SMS.DAL.DAHelpers
{

    public abstract class BaseDA<T> where T : BaseEntity
    {
        private static DBHelper.DataBaseType DataType = DBHelper.DataBaseType.Null;
        private static string _ConnectionString = string.Empty;
        public DBHelper dbHelper = null;

        public BaseDA()
        {
            if (DataType == DBHelper.DataBaseType.Null)
            {
                switch (System.Configuration.ConfigurationManager.AppSettings["DBType"])
                {
                    case "SQL":
                        DataType = SMS.DAL.DAHelpers.DBHelper.DataBaseType.SqlServer;
                        break;
                    case "ORACLE":
                        DataType = SMS.DAL.DAHelpers.DBHelper.DataBaseType.Oracle;
                        break;
                    case "ODBC":
                        DataType = SMS.DAL.DAHelpers.DBHelper.DataBaseType.ODBC;
                        break;
                    case "OLEDB":
                        DataType = SMS.DAL.DAHelpers.DBHelper.DataBaseType.OleDB;
                        break;
                    default:
                        DataType = SMS.DAL.DAHelpers.DBHelper.DataBaseType.SqlServer;
                        break;
                }
            }
            if (string.IsNullOrWhiteSpace(_ConnectionString))
            {
                var xx = System.Net.Dns.GetHostName();
                if (ConfigurationManager.AppSettings["IsLive"] == "1")
                {
                    _ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                }
                else
                    _ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[xx.Replace("-", string.Empty).ToLower()].ConnectionString;
            }
            dbHelper = new DBHelper();
            dbHelper.CreateDBObjects(_ConnectionString, DataType);
        }

        protected void GetNewDBHelper()
        {
            dbHelper = new DBHelper();
            dbHelper.CreateDBObjects(_ConnectionString, DataType);
        }

        public abstract T SaveObject(string userName, T tObject);
        public abstract bool DeleteObject(string userName, T tObject);
        public abstract List<T> GetObjectList(string userName, DateTime fromDate, DateTime toDate);
        public abstract List<T> SearchObject(string userName, string SearchText);

        protected void LogActivity(string userName, string Activity, T tObject)
        {
            try
            {
                GetNewDBHelper();
                dbHelper.AddParameter("@UserName", userName);
                dbHelper.AddParameter("@LogTime", DateTime.Now);
                dbHelper.AddParameter("@Activity", Activity);
                dbHelper.AddParameter("@ObjectString", tObject == null ? string.Empty : tObject.ToObjectString());
                dbHelper.ExecuteNonQuery("USP_LOGUSERACTIVITY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
            }
            catch (Exception ex)
            {
                LogException(userName, ex, tObject);
            }
        }

        protected void LogException(string userName, Exception exception, T tObject)
        {
            try
            {
                GetNewDBHelper();
                dbHelper.AddParameter("@Error", exception.Message);
                dbHelper.AddParameter("@ErrorDescription", string.Format("{0} {1} {2}", exception.InnerException, " Stack Trace :- ", exception.StackTrace));
                dbHelper.AddParameter("@UserName", userName);
                dbHelper.ExecuteNonQuery("USP_INSERT_EXCEPTION", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
            }
            catch (Exception)
            {

                ////throw;
            }
        }

    }
}
